$(document).ready(function appd() {
fenye(1);
})

function fun2(way) {
	$(".box").append(
			"<div class=\"mengban\"></div>" + "<div class=\"update\">"
					+ "<div>" + "	编号 <input type=\"text\" id=\"number\" />"
					+ "</p>" + "商家 <input type=\"text\" id=\"business\" />"
					+ "</p>" + "商品 <input type=\"text\" id=\"commodity\" />"
					+ "</p>" + "</div>"
					+ "<input type=\"button\" value=\"提交\" onclick=\"" + way
					+ "\" /> <input"
					+ " type=\"button\" value=\"关闭\" onclick=\"closes()\" />"
					+ "</div>")

}

function fenye(pages) {
	if(pages == -1){
		alert("没有下一页了");
		return;
	}
	if(pages == -2){
		alert("没有上一页了");
		return;
	}
	
	console.log(pages);
	$.ajax({
		type : "get",
		url : "/order?pages=" + pages,
		async : true,
		success : function(data) {
			console.log(data);
			fun(data.records);
			page(data.pages, data.current);
		}
	});
}

function page(pages, current) {
	$(".pages ul").remove();
	var leftnum = current - 1;
	$(".pages").append(
			"<ul class=\"pagination\">"
					+ "<li class=\"disabled \"><a "
					+ (leftnum <= 0 ? "onclick=fenye("+ -2 +")":"onclick=fenye("+ (current - 1)+")" )
					+ ">&laquo;</a></li>"
					+ "<li><a "
					+ (current + 1 > pages ? "onclick=fenye("+ -1 +")":"onclick=fenye("+ (current + 1)+")" )
					+ " >&raquo;</a></li></ul>")

	for (var i = pages; i >= 1; i--) {
		$(".pagination li:eq(0)").after(
				"<li><a onclick=fenye("+i+")>" + i + "</a></li>");
	}

}

function closes() {
	$(".mengban").remove();
	$(".update").remove();
}

function btnadd() {
	fun2("subadd()");
}

function selectoder() {
	var ordername = $("#ordername").val();
	if (ordername != null) {
		$.ajax({
			type : "get",
			url : "/query/order?ordername=" + ordername,
			async : true,
			success : function(data) {
				console.log(data);
				if (data.length != 0) {
					fun(data);
					return;
				}
				alert("没有此商品！");
			}
		});
	}

}
function subadd() {
	var number = $("#number").val();
	var business = $("#business").val();
	var commodity = $("#commodity").val();
	var order = {
		"number" : number,
		"business" : business,
		"commodity" : commodity
	}
	$.ajax({
		type : "POST",
		url : "/order",
		async : true,
		contentType : "application/json",
		data : JSON.stringify(order),
		success : function(data) {
			console.log(data)
			alert(data.msg);
			window.location.reload();
		}
	});
}

function fun(data) {
	$("tbody tr").remove();
	for (var i = 0; i < data.length; i++) {

		$("tbody")
				.append(
						"<tr>"
								+ "<td>"
								+ data[i].number
								+ "</td>"
								+ "<td>"
								+ data[i].business
								+ "</td>"
								+ "<td>"
								+ data[i].commodity
								+ "</td>"
								+ "<td><span class=\"iconfont icon-shanchu\" onclick=\"del("
								+ data[i].id
								+ ")\"></span>&nbsp;&nbsp; <span class=\"iconfont icon-xiugai\" style=\"font-size: 20px;\" onclick=\"edit("
								+ data[i].id + ",this)\"></span></td>"
								+ "</tr>")
	}

}
function del(id) {
	if (confirm("确定要删除吗？")) {
		$.ajax({
			type : "delete",
			url : "/" + id,
			async : true,
			success : function(data) {
				if (data > 0) {
					alert("删除成功！！！");
					window.location.reload();
				} else {
					alert("删除失败！！！");
				}
			}
		});
	}

}

function edit(id, i) {
	fun2("upOrder(" + id + ")");
	var par = $($(i).parent()).prevAll();
	$("#number").val(par[2].innerText);
	$("#business").val(par[1].innerText);
	$("#commodity").val(par[0].innerText);
}
function upOrder(id) {
	var number = $("#number").val();
	var business = $("#business").val();
	var commodity = $("#commodity").val();
	var order = {
		"number" : number,
		"business" : business,
		"commodity" : commodity,
		"id" : id
	}
	$.ajax({
		type : "put",
		url : "/order",
		async : true,
		contentType : "application/json",
		data : JSON.stringify(order),
		success : function(data) {
			if (data > 0) {
				alert("修改成功！！！");
				window.location.reload();
			} else {
				alert("修改失败！！！");
			}
		}
	});
}
